Completed
Branch master (ebb499)
by Alexey
04:15
created

fastEdit.js ➔ $   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
2
var isEditingEnabled;
3
var widgets = {};
4
5
function toggleEditor(btn) {
6
  if (!isEditingEnabled) {
7
    $.each($('.fastEdit'), function () {
8
      if ($(this).closest('a').length > 0) {
9
        var a = $(this).closest('a');
10
        a.replaceWith("<div class = 'historyA " + a.attr('class') + "' style = '" + a.attr('style') + "' href = '" + a.attr('href') + "'>" + a.html() + "</div>");
11
      }
12
    });
13
    $.each($('.fastEdit'), function () {
14
      $(this).attr('contenteditable', true);
15
      $(this).css('position', 'relative');
16
      html = $(this).html();
0 ignored issues
show
Bug introduced by
The variable html seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.html.
Loading history...
17
      var regex = /<!--start:(.*)-->([\s\S]*?)<!--end:\1-->/ig;
18
      while (match = regex.exec(html)) {
0 ignored issues
show
Bug introduced by
The variable match seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.match.
Loading history...
19
        if (match) {
20
          widgets[match[1]] = match[0];
21
        }
22
      }
23
      $(this).html(html.replace(/<!--start:(.*)-->([\s\S]*?)<!--end:\1-->/ig, '$1'));
24
25
      $(this).ckeditor({
26
        //removePlugins: 'stylescombo',
27
        //startupFocus: true,
28
        customConfig: '/static/moduleAsset/libs/libs/ckeditor/inline_config.js'
29
      });
30
31
    });
32
    isEditingEnabled = true;
33
    $(btn).text('Отключить редактирование');
34
  } else {
35
    $.each($('.fastEdit'), function () {
36
      $(this).ckeditor().editor.destroy();
37
      $(this).removeAttr('contenteditable');
38
      $(this).css('position', 'static');
39
      //$(this).attr('class', 'fastEdit');
40
      for (key in widgets) {
0 ignored issues
show
Bug introduced by
The variable key seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.key.
Loading history...
41
        $(this).html($(this).html().replace(new RegExp(key, "gm"), widgets[key]));
42
      }
43
    });
44
    isEditingEnabled = false;
45
    $.each($('.fastEdit'), function () {
46
      if ($(this).closest('.historyA').length > 0) {
47
        var a = $(this).closest('.historyA');
48
        a.removeClass('historyA');
49
        a.replaceWith("<a class = '" + a.attr('class') + "' style = '" + a.attr('style') + "' href = '" + a.attr('href') + "'>" + a.html() + "</a>");
50
      }
51
    });
52
    $(btn).text('Включить редактирование');
53
  }
54
  return;
55
}
56
$(function () {
57
  $('body').append("<div class ='btn-group' style = 'position:fixed;right:0;top:0;z-index:100000;' ><button onclick='toggleEditor(this);return false;' class ='btn btn-default btn-xs' >Включить редактирование</button></div>");
58
})
59